<?php
namespace App\Services;

class TypeAnalyst
{
    protected $types;
    protected $map = [];

    public function __construct($types)
    {
        $this->types = $types;
        $this->analyze();
    }

    public function getMap()
    {
        return $this->map;
    }

    protected function analyze()
    {
        $vmap = [
            1 => 0, // 生活类
            13 => 1, // 科技类
            18 => 2, // 社会类
            19 => 3, // 历史类
            20 => 4, // 艺术类
            21 => 5, // 体育类
            22 => 6, // 学习类
            23 => 7, // 娱乐类
            24 => 8 // 军事类
        ];
        foreach ($this->types as $type) {
            $this->map[$type->id] = $vmap[$this->findRootType($type)->id];
        }
    }

    protected function findRootType($type)
    {
        if ($type->parent_id === 0) {
            return $type;
        }
        foreach ($this->types as $item) {
            if ($type->parent_id === $item->id) {
                return $this->findRootType($item);
            }
        }
    }
}
